package com.kgc.service.edu.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.service.edu.entity.SieMenu;
import com.kgc.service.edu.entity.SieRole;
import com.kgc.service.edu.entity.SieUser;
import com.kgc.service.edu.mapper.SieMenuMapper;
import com.kgc.service.edu.mapper.SieUserMapper;
import com.kgc.service.edu.service.SieMenuService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SieMenuServiceImpl extends ServiceImpl<SieMenuMapper, SieMenu>  implements SieMenuService {
    private SieMenuMapper sieMenuMapper;

    public SieMenuServiceImpl(SieMenuMapper sieMenuMapper) {
        this.sieMenuMapper = sieMenuMapper;
    }

    @Override
    public PageInfo<SieMenu> selectSieMenu(SieMenu sieMenu, Integer current, Integer size) {
        PageHelper.startPage(current,size);
        List<SieMenu> sieMenus = sieMenuMapper.selectSieMenu(sieMenu);
        PageInfo<SieMenu> pageInfo=new PageInfo<SieMenu>(sieMenus);
        return pageInfo;
    }

    @Override
    public int delectMenuById(List<String> id) {
        int i = sieMenuMapper.delectMenuById(id);
        return i;
    }

    @Override
    public List<SieMenu> tree() {
        List<SieMenu> list = sieMenuMapper.selectSieMenulist();
        return buildTreeMenu(list);
    }

    private List<SieMenu> buildTreeMenu(List<SieMenu> menus) {


        List<SieMenu> finalMenus = new ArrayList<>();

        // 先各自寻找到各自的孩子
        for (SieMenu menu : menus) {

            for (SieMenu e : menus) {
                if (menu.getId() == e.getParent_id()) {
                    menu.getChildren().add(e);
                }
            }

            // 提取出父节点
            if (menu.getParent_id() == "0") {
                finalMenus.add(menu);
            }
        }


        return finalMenus;
    }
}
